package com.cy.pj.sys.service.aspect;

import com.sun.deploy.cache.MemoryCache;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//通过此切面学习spring AOP中各种通知方法的应用
@Aspect
@Component
public class SysTimeAspect {
    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredTime)")
    public void doTime() {
    }


    @Before("doTime()")
    public void doBefore(JoinPoint joinPoint) {
        Object[] objects = joinPoint.getArgs();
        System.out.println(Arrays.toString(objects));
        System.out.println("@Before");
    }
    @Order(2)
    @After("doTime()")
    public void doAfter() {
        System.out.println("@After");
    }

    @AfterReturning("doTime()")
    public void doAfterReturning() {
        MemoryCache.clearLoadedResources();
        System.out.println("@Afterreturning");
    }

    @AfterThrowing("doTime()")
    public void doAfterThrowing() {
        System.out.println("@AfterThrowing");
    }
    @Order(1)
    @Around("doTime()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            System.out.println("@Around.AfterReturning");
            Object result = joinPoint.proceed();
            return result;
        } catch (Throwable e) {
            System.out.println("@Around.AfterThrouwing");
            e.printStackTrace();
            throw e;
        } finally {
            System.out.println("@Around.After");

        }
    }
}















